package com.example.basic.demo.controller;


import com.isyscore.os.dev.api.common.model.builder.GetAllProvinceRequestBuilder;
import com.isyscore.os.dev.api.common.model.builder.GetSimpleWeatherByCityNameRequestBuilder;
import com.isyscore.os.dev.api.common.model.builder.SendMailRequestBuilder;
import com.isyscore.os.dev.api.common.model.builder.SendSmsRequestBuilder;
import com.isyscore.os.dev.api.common.model.domain.MailRequestDomain;
import com.isyscore.os.dev.api.common.model.domain.SmsRequestDomain;
import com.isyscore.os.dev.api.common.model.result.GetAllProvinceResult;
import com.isyscore.os.dev.api.common.model.result.GetSimpleWeatherByCityNameResult;
import com.isyscore.os.dev.api.common.model.result.SendMailResult;
import com.isyscore.os.dev.api.common.model.result.SendSmsResult;
import com.isyscore.os.dev.api.common.service.CommonService;
import com.isyscore.os.dev.api.permission.model.builder.*;
import com.isyscore.os.dev.api.permission.model.domain.PageUserDomain;
import com.isyscore.os.dev.api.permission.model.result.*;
import com.isyscore.os.dev.api.permission.service.PermissionService;
import com.isyscore.os.dev.util.IsyscoreHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/${api-module}/demo")
public class DemoController {


    @Autowired
    private CommonService commonService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 登陆
     */
    @GetMapping("/login")
    public String login(OsLoginRequestBuilder builder) {
        builder.setHeaders(new IsyscoreHashMap());
        OsLoginResult result = permissionService.osLogin(builder);
        if (result.isSuccess()) {
            return result.getResponse().getBody();
        } else {
            return result.getResponse().getMessage();
        }
    }

    /**
     * 获取用户权限
     */
    @GetMapping("/queryUserAcl")
    public String queryUserAcl(HttpServletRequest request, QueryUserAclRequestBuilder builder) {
        builder.setRequestHeader(request);
        QueryUserAclResult result = permissionService.queryUserAcl(builder);
        if (result.isSuccess()) {
            return result.getResponse().getBody();
        } else {
            return result.getResponse().getMessage();
        }
    }

    /**
     * 用户登陆状态
     */
    @GetMapping("/osLoginStatus")
    public String osLoginStatus(HttpServletRequest request, OsLoginStatusRequestBuilder builder) {
        builder.setRequestHeader(request);
        OsLoginStatusResult result = permissionService.osLoginStatus(builder);
        if (result.isSuccess()) {
            return result.getResponse().getBody();
        } else {
            return result.getResponse().getMessage();
        }
    }

    /**
     * 查询用户信息
     */
    @GetMapping("/queryUserById")
    public String queryUserById(HttpServletRequest request, QueryUserByIdRequestBuilder builder) {
        builder.setRequestHeader(request);
        QueryUserByIdResult result = permissionService.queryUserById(builder);
        if (result.isSuccess()) {
            return result.getResponse().getBody();
        } else {
            return result.getResponse().getMessage();
        }
    }

    /**
     * 用户列表分页
     */
    @GetMapping("/pageUser")
    public String pageUser(HttpServletRequest request, PageUserDomain domain) {
        PageUserRequestBuilder builder = new PageUserRequestBuilder();
        builder.setBizModel(domain);
        builder.setRequestHeader(request);
        PageUserResult result = permissionService.pageUser(builder);
        if (result.isSuccess()) {
            return result.getResponse().getBody();
        } else {
            return result.getResponse().getMessage();
        }
    }

    /**
     * 获取天气情况
     */
    @GetMapping("/getSimpleWeatherByCityName")
    public String getSimpleWeatherByCityName(HttpServletRequest request, GetSimpleWeatherByCityNameRequestBuilder builder) {
        builder.setRequestHeader(request);
        GetSimpleWeatherByCityNameResult result = commonService.getSimpleWeatherByCityName(builder);
        if (result.isSuccess()) {
            return result.getResponse().getBody();
        } else {
            return result.getResponse().getMessage();
        }
    }

    /**
     * 获取全部省份
     */
    @GetMapping("/getAllProvince")
    public String getAllProvince(HttpServletRequest request, GetAllProvinceRequestBuilder builder) {
        builder.setRequestHeader(request);
//        builder.setHeaders(new IsyscoreHashMap());
        builder.getHeaders().put("isc-api-version", "2.0");
        GetAllProvinceResult result = commonService.getAllProvince(builder);
        if (result.isSuccess()) {
            return result.getResponse().getBody();
        } else {
            return result.getResponse().getMessage();
        }
    }

    /**
     * 发送短信
     */
    @GetMapping("/sendSms")
    public String sendSms(HttpServletRequest request, SmsRequestDomain domain) {
        SendSmsRequestBuilder builder = new SendSmsRequestBuilder();
        builder.setBizModel(domain);
        builder.setRequestHeader(request);
        SendSmsResult result = commonService.sendSms(builder);
        if (result.isSuccess()) {
            return result.getResponse().getBody();
        } else {
            return result.getResponse().getMessage();
        }
    }

    /**
     * 发送短信
     */
    @GetMapping("/sendMail")
    public String sendMail(HttpServletRequest request, MailRequestDomain domain) {
        SendMailRequestBuilder builder = new SendMailRequestBuilder();
        builder.setBizModel(domain);
        builder.setRequestHeader(request);
        SendMailResult result = commonService.sendMail(builder);
        if (result.isSuccess()) {
            return result.getResponse().getBody();
        } else {
            return result.getResponse().getMessage();
        }
    }


}